package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.xgkj.diyiketang.activity.YuFanDetailActivity;
import com.xgkj.diyiketang.activity.YueFanSJDetailActivity;
import com.xgkj.diyiketang.activity.data.YueFanShangJiaAdapter;
import com.xgkj.diyiketang.data.HuoDongInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.UnRobActivityListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/6/16.
 */
public class Fragment_YueFan_ShangJia extends BaseFragment implements View.OnClickListener, PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener {
    private Context mContext;
    private PullToRefreshListView mListView;
    private YueFanShangJiaAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int paihangbangTaskId = -1;
    private NoDataUtil mNoData;


    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_yuefan_shangjia, null);
        mContext = getActivity();
        initView(view);
        return view;
    }

    private void initView(View view) {

        mListView = (PullToRefreshListView) view.findViewById(R.id.yuefan_shangjia_list);
        mListView.init(mContext, true);
        mAdapter = new YueFanShangJiaAdapter(mContext);
        mListView.setAdapter(mAdapter, true);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListView.getHeaderViewsCount() > 0) {
                    position--;
                }
                if (position < 0 || position >= mAdapter.getCount()) return;
                HuoDongInfo fdinfo = (HuoDongInfo) mAdapter.getItem(position);
                String activeid = fdinfo.getActivityId();
                String icon = fdinfo.getUserLogo();
                String mBusinessLogo = fdinfo.getBusinessLogo();
                Intent bdInent = new Intent(mContext, YueFanSJDetailActivity.class);
                bdInent.putExtra("ACTIVEID", activeid);
                bdInent.putExtra("LOGO", icon);
                bdInent.putExtra("SHANGQUAN", fdinfo.getAreaName());
                startActivity(bdInent);
            }
        });
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
        RelativeLayout contentLayout = (RelativeLayout) view.findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext,contentLayout);
    }


    private void getPaiHaiBang() {
        if (paihangbangTaskId != -1) {
            return;
        }
        if (currentPage == 0) {
            mListView.setNoNextPage();
            return;
        }

        UnRobActivityListTask task = new UnRobActivityListTask(size, currentPage);
        task.addListener(new AnchorListener(mContext));
        paihangbangTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    class AnchorListener extends ShowToastTaskEventListener {
        public AnchorListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == paihangbangTaskId) {
                paihangbangTaskId = -1;
                if (currentPage == 1) {
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                } else {
                    mListView.onLoadDataComplete(true);
                }
                List<HuoDongInfo> fansList = new ArrayList<HuoDongInfo>();
                if (data != null) {
                    fansList = (List<HuoDongInfo>) data;
                }

                if (fansList != null && fansList.size() > 0) {
                    mAdapter.updateData(fansList);
                }

                currentPage = result;
                if (mAdapter != null && mAdapter.getCount() == 0){
                    mNoData.ListViewEmpty(mListView,null,null,-1);
                }else {
                    mNoData.ListViewNoEmpty(mListView);
                }

            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == paihangbangTaskId) {
                paihangbangTaskId = -1;
                if (currentPage == 1) {
                    mListView.onRefreshComplete();
                } else {
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (mAdapter != null && mAdapter.getCount() == 0) {
                mListView.doRefresh();
            }

        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getPaiHaiBang();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getPaiHaiBang();
    }

    @Override
    public void onResume() {
        super.onResume();
        mListView.doRefresh();
    }
}
